/**********常量部分**********/
var registerConstant = {
	    html_txtRequire	: '必须填哦~',
		html_pwdRequire : '密码长度应为6~16个字符',
		html_pwdSoEasy : '密码输入过于简单！',
		html_noEmpty : '只输空格有毛用',
		html_crmPwdDone : '准确无误！',
		html_crmPwdError : '两次填写的密码不一致',
		html_mailDone : '<img src="{THEME_STYLE_PATH}style/images/right.png">',
		html_mailError : '邮箱格式有误，请重新输入',
		html_mailExist : '很不幸，该邮箱已被使用!',
		html_loading :'<img src="{THEME_STYLE_PATH}style/images/login/loading.gif" title="正在检查">',
		html_nameDone : '可以使用!',
		html_nameError : '用户名不符合规则要求!',
		html_nameExist : '很不幸，该昵称已被抢注!',
		html_codeDone : 'NB验证码输入正确!',
		html_codeError : '验证码输入错误',
		reg_pwd : /^[A-Za-z0-9]{6,16}$/,
		reg_mail : /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
		reg_name : /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){2,10}$/
};

/**********函数部分**********/

//密码强度检测
function testpass(password)
{
	var score = 0;
	if (password.length < 6 ) { return -6; }
	//if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2}
	score += password.length * 4;
	score += ( repeat(1,password).length - password.length ) * 1;
	score += ( repeat(2,password).length - password.length ) * 1;
	score += ( repeat(3,password).length - password.length ) * 1;
	score += ( repeat(4,password).length - password.length ) * 1;
	if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;}
	if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;}
	if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;}
	if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;}
	if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;}
	if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;}
	if (password.match(/^\w+$/) || password.match(/^\d+$/) ){ score -= 10;}
	if ( score < 0 ){score = 0;}
	if ( score > 100 ){ score = 100;}
	return score;
	
	function repeat(len,str)
	{
		var res = ""; 
		for (var i = 0; i < str.length; i++ ) 
		{ 
			var repeated = true; 
			for (var j = 0, max = str.length - i - len; j < len && j < max; j++)
			{
				repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len)); 
			} 
			if (j < len) repeated = false;
			if (repeated) { 
				i += len - 1; 
				repeated = false; 
			} 
			else 
			{
				res += str.charAt(i); 
			}
		} 
		return res; 
	}
}

function changeTips(tip,type,html)
{
	var resultTips_wrong = $('#'+tip+'_wrong');
	var resultTips_done= $('#'+tip+'_done');
	//var thisInput = $('#'+tip+'Ipt');
	if(type==0){
		resultTips_wrong.hide();
		resultTips_done.hide();
	}else if(type==1){
		//友好提示
		//resultTips_div.html(html);
		//resultTips_div.hide();
	}else if(type==2){
		//错误结果提示
		resultTips_done.hide();
		resultTips_wrong.html(html);
		resultTips_wrong.show();
	}else if(type==3){
		//正确结果提示
		resultTips_wrong.hide();
		resultTips_done.show();
	}
	
}


/**********开始加载**********/
$(window).load(function(){ 
	var mail = false,name = false,pwd = false,repwd = false,code = false;
	/*邮箱验证begin*/
	$('#jxy_mailIpt').live('focus',function(){
		$('#jxy_mailTips').css('color','#468418');
		changeTips('email',0);return;
	});
	$('#jxy_mailIpt').live('blur',function(){
		$('#jxy_mailTips').css('color','#878787');
		var mailVal = $(this).val();
		if($.trim(mailVal)==''){mail = false;return;}
		if (!registerConstant.reg_mail.exec(mailVal)){
			mail = false;
			changeTips('email',2,registerConstant.html_mailError);
			return;
		}
		changeTips('email',2,registerConstant.html_loading);
		$.ajax({
			url:'index.php?m=Member&c=Index&a=mailcheck',
			type: 'POST',
			data:{act:'regCheck',key:'email',data:$.trim(mailVal)},
			async: true,
			dataType: 'json',
			error: function(){
				alert('服务器忙，请稍后再试 ^-^');
			},
			success:function(result){
				if(result==0){
					changeTips('email',3,registerConstant.html_mailDone);mail = true;return;
				}else{
					changeTips('email',2,registerConstant.html_mailExist);mail = false;return;
				}
			}
		});
	});
	/*邮箱验证end*/



	/*用户名验证begin*/
	$('#nickname').live('focus',function(){
		$('#jxy_nameTips').css('color','#468418');
		changeTips('nickname',0);return;
	});
	$('#nickname').live('blur',function(){
		$('#jxy_nameTips').css('color','#878787');
		var nameVal = $(this).val();
		if($.trim(nameVal)==''){name = false;return;}
		if (!registerConstant.reg_name.exec(nameVal)){
			name = false;changeTips('nickname',2,registerConstant.html_nameError);return;
		}

		$.ajax({
			url:'index.php?m=Member&c=Index&a=checknickname',
			type:'POST',
			data:{act:'regCheck',key:'nickname',data:$.trim(nameVal)},
			//data:{nickname:nameVal},
			async: true,
			dataType: 'json',
			error: function(){
				alert('服务器忙，请稍后再试 ^-^');
			},
			success:function(result){
				if(result == 0){
					changeTips('nickname',3,registerConstant.html_nameDone);name = true;return;
				}else{
					changeTips('nickname',2,registerConstant.html_nameExist);name = false;return;
				}
			}
		});
	});
	/*用户名验证end*/


	/*密码验证begin*/
	var friendTip_div = $('#mainPwdTips');var pwdStatus_div = $('#mainPwdStatus');var resultPwdTip_div = $('#m_mainPwd');
	var friendCrmTip_div = $('#mainCfmPwdTips');var resultCrmPwd_div = $('#m_mainCfmPwd');

	$('#jxy_mainPwdIpt').live('focus',function(){
		$('#jxy_mainPwdTips').css('color','#468418');
		changeTips('password',0);return;
	});

	$('#jxy_mainPwdIpt').live('blur',function(){
		var pwd_v = $(this).val();
		$('#jxy_mainPwdTips').css('color','#878787');
		if(pwd_v.length!=0 && $.trim(pwd_v).length==0){
			pwd = false;$(this).val($.trim(pwd_v));
			//changeTips('mainPwd',2,registerConstant.html_noEmpty);
		}else if(!registerConstant.reg_pwd.test(pwd_v)){
			pwd = false;
			$(this).val($.trim(pwd_v));changeTips('password',2,registerConstant.html_pwdRequire);
		}else if(pwd_v=="123456" || pwd=="111111" || pwd_v=="000000"){
			pwd = false;
			$(this).val($.trim(pwd_v));changeTips('password',2,registerConstant.html_pwdSoEasy);
		}else{
			$('#jxy_mainPwdIpt').val($.trim(pwd_v));
			changeTips('password',3,registerConstant.html_crmPwdDone);pwd = true;
		}
		
		var crmPwd_v = $.trim($('#jxy_mainCfmPwdIpt').val());
		if($(this).val()=='' || crmPwd_v.length==0 || pwd==false){
			repwd = false;$('#jxy_mainCfmPwdIpt').val($.trim(crmPwd_v));
		}else if(crmPwd_v.length!=0 && crmPwd_v!='' && pwd_v!=crmPwd_v){
			repwd = false;changeTips('pwdconfirm',2,registerConstant.html_crmPwdError);
		}else if(pwd==true){
			$('#jxy_mainCfmPwdIpt').val($.trim(crmPwd_v));
			changeTips('pwdconfirm',3,registerConstant.html_crmPwdDone);repwd = true;
		}
	});

	
	$('#jxy_mainCfmPwdIpt').live('focus',function(){
		$('#jxy_mainCfmPwdTips').css('color','#468418');
		changeTips('pwdconfirm',0);return;
	});

	$('#jxy_mainCfmPwdIpt').live('blur',function(){
		$('#jxy_mainCfmPwdTips').css('color','#878787');
		var pwd_v = $('#jxy_mainPwdIpt').val();var crmPwd_v = $.trim($('#jxy_mainCfmPwdIpt').val());
		if(pwd){
			if($(this).val()=='' || crmPwd_v.length==0){
				repwd = false;$('#jxy_mainCfmPwdIpt').val($.trim(crmPwd_v));
			}else if(crmPwd_v.length!=0 && crmPwd_v!='' && pwd_v!=crmPwd_v){
				repwd = false;changeTips('pwdconfirm',2,registerConstant.html_crmPwdError);
			}else{
				$('#jxy_mainCfmPwdIpt').val($.trim(crmPwd_v));
				changeTips('pwdconfirm',3,registerConstant.html_crmPwdDone);repwd = true;
			}
		}
	});
	/*密码验证end*/
	
	
	/*验证码begin*/
	$('#jxy_codeIpt').live('keyup',function(){
		$(this).val($(this).val().toUpperCase()); 
	});
	$('#jxy_codeIpt').live('focus',function(){
		$('#jxy_codeIpt .txt-tips').css('color','#468418');
		changeTips('verify',1,'');return;
	});
	$('#vCode_pic').bind('click',function(){
		$(this).attr('src',$("#vCode_pic").attr('src') + '&t=' + Math.random());
	}); 
	$('.yzmcg').bind('click',function(){
		$('#vCode_pic').attr('src',$("#vCode_pic").attr('src') + '&t=' + Math.random());
	});
	$('#jxy_codeIpt').live('blur',function(){
		var code_v = $.trim($(this).val());
		if(code_v!=''){
			$.ajax({
				url:'index.php?m=Member&c=Index&a=checkverify',
				type:'POST',
				data:{'act':'codeCheck','verify':code_v},
				async: true,
				dataType: 'json',
				error: function(){
					code = false;
					alert('服务器忙，请稍后再试 ^-^');
				},
				success:function(result){
					if(result == 1){
						code = true;
						changeTips('verify',3,registerConstant.html_codeDone);return;
					}else{
						code = false;
						changeTips('verify',2,registerConstant.html_codeError);return;
					}
				}
			});
		}else{
			code = false;
		}
	});
	/*验证码end*/




	/*确认注册begin*/
	$('.loginbtn').unbind('click').live('click',function(){
		if(!mail){
			var mailVal = $('#jxy_mailIpt').val();
			if($.trim(mailVal)==''){
				$('#jxy_mailIpt').val('');
				changeTips('email',2,registerConstant.html_txtRequire);
			}
		}
		if(!name){
			var nameVal = $('#jxy_nameIpt').val();
			if($.trim(nameVal)==''){
				$('#jxy_nameIpt').val('');
				changeTips('nickname',2,registerConstant.html_txtRequire);
			}
		}
		if(!pwd){
			var pwdVal = $('#jxy_mainPwdIpt').val();
			if($.trim(pwdVal)==''){
				$('#jxy_mainPwdIpt').val('');
				changeTips('password',2,registerConstant.html_txtRequire);
			}
		}
		if(!repwd){
			var crmPwd_v = $.trim($('#jxy_mainCfmPwdIpt').val());
			if($.trim(crmPwd_v)==''){
				$('#jxy_mainCfmPwdIpt').val('');
				changeTips('pwdconfirm',2,registerConstant.html_txtRequire);
			}
		}
		//code = true;
		if(!code){
			var code_v = $.trim($('#jxy_codeIpt').val());
			if($.trim(code_v)==''){
				$('#jxy_codeIpt').val('');
				changeTips('verify',2,registerConstant.html_txtRequire);
			}
		}
		if(mail && name && pwd && repwd && code){
			$('#register').submit();
		}else{
			//alert('请把信息填写完整！');
		if(!mail) $('#jxy_mailIpt').focus();
			else if(!name) $('#jxy_nameIpt').focus();
			else if(!pwd) $('#jxy_mainPwdIpt').focus();
			else if(!repwd) $('#jxy_mainCfmPwdIpt').focus();
			else if(!code) $('#jxy_codeIpt').focus();
		}
	});
	
	
	/*确认注册end*/
});